/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package FlightSelect;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.*;
import flight.FlightBean;
import java.util.StringTokenizer;
import javax.servlet.RequestDispatcher;

/**
 *
 * @author cipher
 */
public class FlightSelectServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String FlightIDs = request.getParameter("choose_flight");
        FlightBean fb = (FlightBean)request.getSession().getAttribute("fb");
        int TicketNum = fb.getNoOfTickets();
//        String TripType = request.getParameter("triptype");
        String Rst = request.getParameter("reset");

        FlightSelectBean fsb = new FlightSelectBean();
        StringTokenizer st = null;

        fsb.setReset(Integer.parseInt(Rst));

        st = new StringTokenizer(FlightIDs,".");
        if (st.hasMoreTokens()) {
            fsb.setFlightID1(Integer.parseInt(st.nextToken()));
            fsb.setHoop(1);
            if (st.hasMoreTokens()) {
                fsb.setFlightID2(Integer.parseInt(st.nextToken()));
                fsb.setHoop(2);
                if (st.hasMoreTokens()) {
                    fsb.setFlightID3(Integer.parseInt(st.nextToken()));
                    fsb.setHoop(3);
                    if (st.hasMoreTokens()) {
                        fsb.setFlightID4(Integer.parseInt(st.nextToken()));
                        fsb.setHoop(4);
                        if (st.hasMoreTokens()) {
                            fsb.setFlightID5(Integer.parseInt(st.nextToken()));
                            fsb.setHoop(5);
                        }
                    }
                }
            }
        }
        else {
            fsb.setFlightID1(Integer.parseInt(FlightIDs));
            fsb.setHoop(1);
        }

        try {
            FlightSelectPageBean fspb = new FlightSelectPageBean();

            String Email = (String)request.getSession().getAttribute("userid");
            
            fspb.setFsb(fsb);      // set searching information bean

            PageBean pageCtl = fspb.Proceed(TicketNum,0,Email, null, 0, 0, 0);
            int pricetotal = fspb.getPrice();
            request.setAttribute("pricetotal",pricetotal);
            request.setAttribute("pageCtl",pageCtl); // store PageBean object in request
            request.setAttribute("fb",fb);
            request.setAttribute("departFlight",fsb);
//            session.setAttribute("pageCtl1",pageCtl);

            String cityString = "";

            cityString = fspb.getCityString();

            cityString = cityString.substring(0, cityString.length()-1);
            
            request.setAttribute("cityString",cityString);
        }
        catch(Exception e) {
            e.printStackTrace();
        }

//        if (TripType.equals("1")) {//single trip
            RequestDispatcher dis = request.getRequestDispatcher("/rightpanels/booking_rightpanel.jsp");
            dis.forward(request,response);
//        }else {//round trip
//            RequestDispatcher dis = request.getRequestDispatcher("/rightpanels/results_return_flight_right.jsp");
//            dis.forward(request,response);
//        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
